package com.ninjia.mvpproject.application;

import android.app.ActivityManager;
import android.app.Application;
import android.content.Context;

import com.ninjia.mvpproject.BuildConfig;
import com.ninjia.mvpproject.utils.AppActivityManager;
import com.ninjia.mvpproject.utils.swipeback.ActivityLifecycleHelper;
import com.socks.library.KLog;

/**
 * 作者：Android on 2017/8/1
 * 邮箱：365941593@qq.com
 * 描述：
 */

public class AppConfig extends Application {
    public static AppConfig instance;
    //滑动返回
    public ActivityLifecycleHelper mActivityLifecycleHelper;

    public AppActivityManager mAppActivityManager;

    private AppComponent mAppComponent;

    public AppConfig() {

    }

    @Override
    public void onCreate() {
        super.onCreate();
        instance = this;
        KLog.init(BuildConfig.LOG_DEBUG);
        setupApplicationComponent();
        registerActivityLifecycleCallbacks(ActivityLifecycleHelper.build());
        mAppActivityManager = new AppActivityManager(this);
    }

    public static synchronized AppConfig getInstance() {
        if (null == instance) {
            instance = new AppConfig();
        }
        return instance;
    }

    protected void setupApplicationComponent() {
        mAppComponent = DaggerAppComponent
                .builder()
                .appModule(new AppModule(this))
                .aPIModule(new APIModule(this))
                .build();
        mAppComponent.inject(this);
    }
    public AppComponent getApplicationComponent() {
        return mAppComponent;
    }

    public static String getCurProcessName(Context context) {
        int pid = android.os.Process.myPid();
        ActivityManager activityManager = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
        for (ActivityManager.RunningAppProcessInfo appProcess : activityManager.getRunningAppProcesses()) {
            if (appProcess.pid == pid) {
                return appProcess.processName;
            }
        }
        return null;
    }
}
